library(knitr)
library(tidyverse)
library(sf)
library(sp)
library(tmap)
library(skimr)
library(rnaturalearth)
library(readxl)
#I am calculating the number of characters in the work 'elephant'
num <- nchar("elephant")
Comments are important for differentiating between R commands and regular text within a console. Comments tell R not to interpret that part of the information as command but to ignore it. Adding comments help to document the things we do for future reference. Comments are also useful for excluding pieces of code that we do not want to run.
Adding a photo
A beautiful landscape, image from: By Ivar Leidus - Own work, CC BY-SA 3.0, https://commons.wikimedia.org/w/index.php?curid=29024401
I chose to include a picture of a beautiful landscape because I really like natural landscapes.
echo=FALSE was added in the code chunk in order to make sure that the code for inserting the picture does not show up in the resulting html when I knit, and that only the output (picture) is visible.
About fireflies
Fireflies are beetles that use a chemical reaction in their abdomen to emit light. These lights are often seen as flashes that come on and off in darkness as thy fly. Depending on the species or group, some fireflies will emit the light in quick flashes, others in long lasting glows and others produce a light that is invisible to the human eye. It is believed that the flashing patterns are a way that fireflies communicate or attract mates.
A firefly, image from: art farmer from evansville indiana, usa, CC BY-SA 2.0 https://creativecommons.org/licenses/by-sa/2.0, via Wikimedia Commons
Why study fireflies?
Fireflies are highly beneficial insects because they play a great role as pollinators. Fireflies feed on pollen and nectar and are therefore very effective pollinators as they move from flower to flower.
The aim of the firefly watch study
The Firefly Watch project aims to map firefly locations. These maps help to determine changes in firefly populations and understand the factors that could lead to any such changes. Anyone can participate in the project by observing and reporting firefly sightings in their backyard. To participate, one need to first identify a suitable location/habitat at which to observe fireflies.
They also need to create an account at this site where they submit observations. Reports should include the number of flashing fireflies observed over the course of 10 minutes in three 10 second periods and the number of flashing patterns seen. Participants are also required to provide additional details about the location of observation including:
The firefly data set contains 20134 locations where people looked for fireflies and 12 attributes related to each observation location.
How many observations are there? (how many rows)
nrow(firefly)
## [1] 20134
There are rnrow(firefly)` observations of fireflies.
What is the unit of observation (e.g. what does each row represent?)
The unit of observation is a person’s (observer) firefly sightings at a selected location and specific time.
What variables do we have? E.g. what columns do we have for each observation?
The variables contain more information related to the specific observation location. These include:
names(firefly)
## [1] "Year" "Month" "Day"
## [4] "Latitude" "Longitude" "State"
## [7] "Country" "Location" "Backyard"
## [10] "Temperature_F" "Number.Seen.in.10.s" "Clouds"
Where:
Which years do we have data for? How many observations in each year?
These are the years for which we have data for:
table(firefly$Year)
##
## 2008 2009 2010 2011 2012 2013 2014 2015 2016
## 1542 2892 2537 2822 2590 1824 1720 1993 2214
Is Pennsylvania included in the dataset? How many observations were taken in PA?
'PA' %in% firefly$State
## [1] TRUE
Pennsylvania is included in the dataset.
table(firefly$State)
##
## AL AR AZ CA CO CT DC DE FL GA IA IL IN KS KY LA
## 117 162 8 4 45 271 33 28 253 322 143 2840 312 139 212 217
## MA MD ME MI MN MO MS NC NE NH NJ NM NY OH OK PA
## 2089 636 156 1062 221 530 79 1297 137 351 655 7 1091 1206 101 1728
## RI SC SD TN TX UT VA VT WA WI WV WY
## 164 302 15 226 689 20 1606 151 1 256 246 6
We have 1728 observations for Pennsylvania.
Second row and the 4th column
firefly[2,4]
The value of the second row and the 4th column is a latitude location, 42.3683 degrees
The rows of data that are in April.
april.firefly <- filter(firefly, Month==4)
Only keep data where temperature is below 200 F (more realistic values)
firefly <- filter(firefly, Temperature_F < 200)
Based on tutorial 11a, it is not possible to plot data that is in lat/lon projection on a map with country borders in UTM Zone because these are two different coordinate systems that use different units to place items on the surface of the earth. The lat/long coordinate system uses degrees and UTM uses meters.
The firefly data is marked. This is because it contains 10 attributes (excluding lat/lon coordinates) that provide us more information about the location where fireflies were observed. In this dataset, example marks include temperature, cloud conditions, year, month, and day of observation, whether or not the location was in a backyard.
The st_geometry() command tell R to ignore marks when plotting data that is marked. This ignores all the marks and only plots the locations of points (if point data) or borders of polygon (if polygon data).
Converting firefly data to sf
firefly.sf <- st_as_sf(firefly,coords = c("Longitude", "Latitude"),crs=4326)
Loading rnaturalearth state-boundaries for US States and convert to projection 4325 (geographic)
states.sf <- ne_states(country="united states of america",returnclass = "sf")
# Transform to 4326
states.sf <- st_transform(states.sf,crs=4326)
A basic map of the firefly locations
plot(st_geometry(firefly.sf),
pch=16,
col=rgb(0.6,0.2,1,.5),#semi-transparent purple
#col=rgb(0,0,1,.5),
cex=.5,
main="Firefly locations")
plot(st_geometry(states.sf),add=TRUE)

Using “tmap” to make pretty plots
tmap_mode("plot") # Set the static plot mode
myplot <- tm_shape(firefly.sf) + # Load the firefly data
tm_dots(col="black", size=0.05) + # Plot it as dots
tm_shape(states.sf) + # Load the state borders
tm_borders(lwd=.5) # Plot them as just borders
myplot

Turning on the interactive view mode
tmap_mode("view")
myplot